<template>
    <page-wrapper>
        <table-search @search="search(1)" @reset="reset()">
            <template slot="form">
                <form-item label="">
                    <a-input v-model.trim="form.factoryName" placeholder="厂区名称" />
                </form-item>
                <form-item label="">
                    <a-select v-model="form.open" style="width:120px;" allowClear placeholder="请选择状态">
                        <a-select-option :value="0">停用</a-select-option>
                        <a-select-option :value="1">正常</a-select-option>
                    </a-select>
                </form-item>
            </template>
            <template slot="extra">
                <a-button class="filter-item" type="primary" :disabled="hasPerm(['sys:factory:add'])" icon="plus" @click="addInfo()">新增厂区</a-button>
                <a-button
                    class="filter-item btn_space"
                    type="danger"
                    @click="remove()"
                    :disabled="!selectionBox.length || hasPerm(['sys:factory:remove'])"
                    icon="delete"
                    >删除</a-button
                >
            </template>
        </table-search>
        <table-list
            :loading="loading"
            :columns="columns"
            rowKey="factoryId"
            :pagination="pagination"
            :selection="selection"
            :data="list"
        >
            <template slot="factoryLeader" slot-scope="{ text }">
                <field-tag
                    :value="text"
                    type="userId"
                    showType="userName"
                    :data="userList"
                ></field-tag>
            </template>
            <template slot="open" slot-scope="{ text }">
                <a-switch
                    checked-children="开启"
                    un-checked-children="关闭"
                    :key="text.factoryId"
                    :default-checked="text.open==1 ? true : false"
                    @change="onSwitchStatus(text)"
                />
            </template>
            <template slot="action" slot-scope="{ text }">
                <a-button size="small" type="primary" class="btn_space" :disabled="hasPerm(['sys:factory:update'])" @click="addInfo(text)">编辑</a-button>
                <a-button size="small" type="danger" class="btn_space" :disabled="hasPerm(['sys:factory:remove'])" @click="remove(text)">删除</a-button>
            </template>
        </table-list>

        <template v-if="isShowInfo">
            <factory-info :info="itemInfo" :userList="userList" @cancel="isShowInfo = false" @submit="submit" />
        </template>
        
    </page-wrapper>
</template>
<script>
import table from '@/mixins/table';
import { columns } from './columns';
import FactoryInfo from './components/info';
import userApi from '@/api/system/user'
import {remoteList,updateInfo,deleteInfo} from '@/api/system/factory'

export default {
    name: 'UserList',
    mixins: [table],
    components: {
        FactoryInfo
    },
    data() {
        let originForm = {
            factoryName: null,
            open: undefined
        };
        return {
            originForm,
            form: {
                ...originForm
            },
            columns,
            userList:[],
            itemInfo: null,
            isShowInfo: false
        };
    },
    async created(){
        await this.getSource();
        this.search(1)
    },
    methods: {
        async search(page) {
            try {
                this.loading = true;
                if(page){
                    this.form.page = page
                }
                this.list = []
                let res = await remoteList(this.form);
                let { list, total } = res.data;
                this.list = list;
                this.total = total;
                this.selectionBox = []
            } finally {
                this.loading = false;
            }
        },
        //删除
        remove(item = '') {
            this.$confirm({
                title: '提示',
                content: '确定要删除厂区？',
                okText: '确定',
                cancelText: '取消',
                onOk:async ()=>{
                    try{
                        let ids=[];
                        if (item) {
                            ids = [item.factoryId];
                        } else {
                            let selectionBox = this.selectionBox;
                            selectionBox.map(async item => {
                                ids.push(item.factoryId);
                            });
                        }
                        await deleteInfo({ids});
                        this.$message.success('删除成功!');
                        this.search(1);
                    }catch(err){

                    }
                },
                onCancel() {}
            });
        },
        //是否开启
        async onSwitchStatus(item) {
            try {
                let { open } = item;
                item.open = open==1 ? 0 : 1;
                this.loading = true;
                await updateInfo(item);
                this.$message.success('操作成功!');
            } catch (err) {
                let { open } = item;
                item.open = open ? 0 : 1;
            } finally {
                this.loading = false;
            }
        },
        //显示添加用户
        addInfo(item = {}) {
            this.isShowInfo = true;
            this.itemInfo = item;
        },
        submit() {
            this.isShowInfo = false;
            this.itemInfo = {}
            this.search(1);
        },
        async getSource(){
            let userRes = await userApi.remoteList({page:1,size:1000});
            this.userList = userRes.data.list;
        }
    }
};
</script>
